package visiopuce.DAO;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Query;

import visiopuce.HibernateUtil;
import visiopuce.objets.Biomedecine;
import visiopuce.objets.Puce;
import visiopuce.objets.TypePuce;

public class PuceDAOImpl extends GenericDAOImpl<Puce, BigDecimal> implements PuceDAO {
	/** Constructeur priv� */
	private PuceDAOImpl() {
	}

	/** Instance unique non pr�initialis�e */
	private static PuceDAOImpl INSTANCE = null;

	/** Point d'acc�s pour l'instance unique du singleton */
	public static PuceDAO getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new PuceDAOImpl();
		}
		return INSTANCE;
	}

	public List<Puce> findAllByidAnalyse(Integer idAnalyse) {
		List<Puce> puces = null;
		String sql = "SELECT p FROM Puce p WHERE p.analyse.idAnalyse = :idAnalyse";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idAnalyse", idAnalyse);
		puces = findMany(query);
		return puces;
	}

	@Override
	public boolean isPucePrincipale(int idPuce) {
		boolean isprincipale = false;
		String sql = "SELECT p.analyse.prelevement.personne.idPucePrincipale FROM Puce p WHERE p.idPuce = :idPuce";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idPuce", idPuce);
		Object o = query.uniqueResult();
		if (o != null && (int) o == idPuce) {
			isprincipale = true;
		}
		return isprincipale;
	}

	@Override
	public Biomedecine getIndicationByIdPuce(Integer idPuce) {
		Biomedecine r = null;
		String sql = "SELECT p.analyse.prelevement.indication FROM Puce p WHERE p.idPuce = :idPuce";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idPuce", idPuce);
		Object o = query.uniqueResult();
		if (o != null && o instanceof Biomedecine) {
			r = (Biomedecine) o;
		}
		return r;
	}

	@Override
	public String getSexePersonneByIdPuce(Integer idPuce) {
		String r = "";
		String sql = "SELECT p.analyse.prelevement.personne.sexe FROM Puce p WHERE p.idPuce = :idPuce and p.analyse.prelevement.secteur.idSecteur in (8,9)";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idPuce", idPuce);
		Object o = query.uniqueResult();
		if (o != null && o instanceof String) {
			r = (String) o;
		}
		return r;
	}

	@Override
	public boolean getIsPatientByIdPuce(Integer idPuce) {
		boolean r = false;
		String sql = "SELECT p.analyse.prelevement.personne.patient  FROM Puce p WHERE p.idPuce = :idPuce";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idPuce", idPuce);
		Object o = query.uniqueResult();
		if (o != null && o instanceof Boolean) {
			r = (Boolean) o;
		}
		return r;
	}

	@Override
	public int getIdPersonneByIdPuce(Integer idPuce) {
		int r = 0;
		String sql = "SELECT p.analyse.prelevement.personne.idPersonne  FROM Puce p WHERE p.idPuce = :idPuce";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idPuce", idPuce);
		Object o = query.uniqueResult();
		if (o != null && o instanceof Integer) {
			r = (int) o;
		}
		return r;
	}

	@Override
	public TypePuce getTypePuceByIdPuce(Integer idPuce) {
		TypePuce tp = null;
		String sql = "SELECT p.typePuce  FROM Puce p WHERE p.idPuce = :idPuce";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idPuce", idPuce);
		Object o = query.uniqueResult();
		if (o != null && o instanceof TypePuce) {
			tp = (TypePuce) o;
		}
		return tp;
	}

	@Override
	public HashMap<String, Puce> getPuceFromFeuillePuce(int idFeuillePuce) {
		List<Puce> puces = null;
		String sql = "SELECT p FROM Puce p WHERE p.feuillePuce.idFeuillePuce = :idFeuillePuce ORDER BY p.ordreFeuille";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idFeuillePuce", idFeuillePuce);
		puces = findMany(query);
		HashMap<String, Puce> puceshm = new  HashMap<String, Puce>();
		for (Puce puce : puces) {
			puceshm.put(puce.getOrdreFeuille(), puce);
		}
		return puceshm;
	}

	@Override
	public String getNumeroPrelevementByidPuce(Integer idPuce) {
		String r = "";
		String sql = "SELECT p.analyse.prelevement.numeroPrelevement FROM Puce p WHERE p.idPuce = :idPuce";
		Query query = HibernateUtil.getSession().createQuery(sql).setParameter("idPuce", idPuce);
		Object o = query.uniqueResult();
		if (o != null && o instanceof String) {
			r = (String) o;
		}
		return r;
	}
}